<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/6 0006
 * Time: 18:17
 */
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\Active;
use app\common\model\AdTemplate;
use app\common\model\Content;
use app\common\model\Crontab;
use app\common\model\Forum;
use app\common\model\IndexColumn;
use app\common\model\Knowledge;
use app\common\model\NewFlash;
use app\common\model\Platform;
use think\Config;
use think\Request;
use app\common\model\Goods as GoodsModel;
use think\Session;

class Goods extends AdminBase
{

    /**
     * @return mixed
     * 列表
     */
    public function index(Request $request)
    {
        $keyword = $request->param('keyword') ?: '';
        $page    = $request->param('page') ?: 1;
        $limit   = $request->param('limit') ?: 15;
        $where   = [];
        if ($keyword) {
            $where['goods_name'] = ['like', "%{$keyword}%"];
        }
        $goods_list = GoodsModel::where($where)->order(['goods_id' => 'DESC'])->paginate($limit, false,
            [
                'page' => $page,
                'query' => [
                    'keyword' => $keyword
                ]
            ]);

        foreach ($goods_list as $item => $value) {
            $goods_list[$item]['show_img'] = explode(',', $value['original_img'])[0];
        }

        return $this->fetch('list', ['goods_list' => $goods_list, 'keyword' => $keyword]);
    }

    /**
     * @return mixed
     * 添加商品
     */
    public function add()
    {
        return view('add');
    }


    /**
     * @param Request $request
     * 商品保存
     */
    public function save(Request $request)
    {

        $data['goods_name']    = $request->param('goods_name');
        $data['start_price']   = $request->param('start_price');
        $data['raise_price']   = $request->param('raise_price');
        $data['market_price']  = $request->param('market_price');
        $data['shop_price']    = $request->param('shop_price');
        $data['goods_content'] = $request->param('content') ?: '';
        $data['original_img']  = $request->param('original_img');
//        $data['detail_img']    = $request->param('original_img_other');
//        $data['detail_img']     = $request->param('detail_img');
        $data['is_on_sale']     = $request->param('is_on_sale');
        $data['sales_end_time'] = strtotime($request->param('end_time'));


        $res = GoodsModel::create($data);

        if ($res) {
            //推入定时任务
            Crontab::create(['goods_id' => $res->goods_id, 'end_time' => $data['sales_end_time']]);
            $action = "商品ID" . $res->goods_id;
            $this->getLog("1", $action);
            $this->success('添加成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * @param Request $request
     * @return \think\response\Json
     *
     */
    public function updateOnlineType(Request $request)
    {
        $status = $request->param('status');
        $id     = $request->param('id');

        $result = GoodsModel::where('goods_id', $id)->update(['is_on_sale' => $status]);
        if ($result) {
            return json(['status' => 1]);
        }
        return json(['status' => 0]);
    }


    /**
     * @param Request $request
     * 商品编辑
     */

    public function edit(Request $request)
    {
        $id                         = $request->param('id');
        $goods_info                 = GoodsModel::get($id);
        $goods_info['img_arr']      = explode(',', $goods_info['original_img']);
        $goods_info['img_arr_info'] = explode(',', $goods_info['detail_img']);


        return view('edit', compact('goods_info'));
    }

    /**
     * @param Request $request
     * 商品编辑提交
     */
    public function update(Request $request)
    {
        $data['goods_name']     = $request->param('goods_name');
        $data['start_price']    = $request->param('start_price');
        $data['raise_price']    = $request->param('raise_price');
        $data['market_price']   = $request->param('market_price');
        $data['shop_price']     = $request->param('shop_price');
        $data['goods_content']  = $request->param('content') ?: '';
        $data['original_img']   = $request->param('cover_arr');
        $data['is_on_sale']     = $request->param('is_on_sale');
        $data['sales_end_time'] = strtotime($request->param('end_time'));
        $id                     = $request->param('id');
        $res                    = GoodsModel::where('goods_id', $id)->update($data);
        if ($res) {

            //删除原来的定时任务
            Crontab::destroy(Crontab::where('goods_id', $id)->value('id'));

            //重新推入定时任务
            Crontab::create(['goods_id' => $id, 'end_time' => $data['sales_end_time']]);
            $action = "商品ID" . $id;
            $this->getLog("3", $action);
            $this->success('修改成功');
            $this->redirect('goods/edit', ['id' => $id]);
        } else {
            $this->error('修改失败');
        }
    }

    /**
     * 删除商品
     */
    public function delete(Request $request)
    {
        $id = $request->param('id');
        //删除多条
        if (empty($id)) {
            $ids = $_POST['ids'];
            foreach ($ids as $k => $v) {
                GoodsModel::destroy($v);
                $action = "商品ID" . $v;
                $this->getLog("2", $action);
            }
            $this->success('删除成功');
        } else {//单条
            if (GoodsModel::destroy($id)) {
                $action = "商品ID" . $id;
                $this->getLog("2", $action);
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        }

    }


}


